﻿#pragma once
#include "GLLObj.h"
#include "GLLObjPrprty.h"

namespace GLL
{
	class ObjectAnimator;
	class Level;

	/**隶属于场景的物体.*/
	class LvlObj : public Object, public ObjectProperty
	{
	public:
		Level* GetMgr(){return (Level*)Object::GetMgr();}

		/**通过智能指针管理.
		因为在Level的外部无法得到LvlObj*，
		所以代码中并没有机会去delete LvlObj
		*/
		virtual ~LvlObj();

		/**.*/
		void PutToLoadQueue();

	public:
		/**一个物体可以同时有多个生命状态，所以需要按位操作.
		如：当物体正在Load的时候，被其他线程删除*/
		enum LifeStatus
		{
			LS_New = 0, //独占
			LS_Created = 1, //独占

			LS_PreLoading = 2,
			LS_PreLoaded = 4,//独占

			LS_Loading = 8,
			LS_Loaded  = 16, //独占

			LS_Unloading = 32,
			LS_Unloaded = 64, //独占

			LS_PostUnloading = 128,
			LS_Over = 256,
		};

		bool NotLoaded(){return !HasLifeStatus(LS_Loaded);}
		bool HasLifeStatus(LifeStatus ls);
		void SetLifeStatus(LifeStatus ls);
		void RemoveLifeStatus(LifeStatus ls);

	protected:
		/**子类实现.*/
		virtual bool OnUpdate_LvlObj(TimeMS){return true;}

		/**只能由Level来创建.*/
		LvlObj();

	private:
		/**每帧更新.
		子类不要覆盖*/
		virtual bool OnUpdate(TimeMS);

	protected:
		LifeStatus _lifeStatus;
		LifeStatus _lastSet;
		GLL_Mutex _statusMutex;///<状态锁.
		GLL_Mutex _chgLS_PrePost;
		GLL_Mutex _chgLS_LoadUnload;

		friend class Level;
		friend class LevelThreadPost;
		friend class LevelThreadUnload;
		friend class LevelThreadPostLoad;
		friend class LevelThreadLoad;
		friend class LevelThreadPreLoad;
	};

	typedef boost::shared_ptr<LvlObj> LvlObjSrdPtr;

};//GLL

namespace boost{
	void checked_delete(GLL::Level* x);
}
